$(document).ready(function() {
     
    $('input[type="text"]').focus(function() {
        $(this).css("color", "#555555");
        this.select();
//        if (this.value == this.defaultValue) {
//            $(this).val("");
//        } else if (this.value != this.defaultValue) {
//            this.select();
//        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');

        }
    });
    uploadFile();
    //xu li import file excel product
    $("form.form_upload_file").submit(function() {
        $('#upload_target').unbind().load(function() {
            // get content from hidden iframe
            var body = $('#upload_target').contents().find('body ').html();

            // proof content if there is an error
            if (body.indexOf("uperror") != -1) {
                var error = $('#upload_target').contents().find('p.uperror ').html();
                alert(error);

            }
            else {
                var msg = $('#upload_target').contents().find('p.msg').html();
                alert(msg);

            }
        });

    });
    //xu li upload main photo từ máy tính
    $("form.form_upload_main_photo").submit(function() {
        $('#upload_main_photo').unbind().load(function() {
            // get content from hidden iframe
            var body = $('#upload_main_photo').contents().find('body ').html();
            // proof content if there is an error
            if (body.indexOf("uperror") != -1) {
                var error = $('#upload_main_photo').contents().find('p.uperror ').html();
                alert(error);
            }
            else {
                var folder_temp = $('#upload_main_photo').contents().find('p.folder_temp').html();
                var file_name_temp = $('#upload_main_photo').contents().find('p.file_name_temp').html();
                var thumb_url = $('#upload_main_photo').contents().find('p.thumb_url').html();
                $("#folder_temp").val(folder_temp);
                $("#file_main_photo_temp").val(file_name_temp);
                $("#show_main_photo").html('<img src="' + thumb_url + '" width=200px />').show();
            }

        });
    });
    $("#btn_delete_main_photo").click(function() {
        $("#folder_temp").val('');
        $("#file_main_photo_temp").val('');
        $("#show_main_photo").hide();
        $(".filename", "#form_upload_main_photo").html("No file selected");
    });
    //upload main photo từ url
    var upload_photo = false;
    $('#btn_upload_main_photo').click(function(e) {
        if (upload_photo === false) {
            upload_photo = true;
            if ($("#url_main_photo").val() != "") {
                $.post("ajax/uploadMainPhoto.php", {
                    main_photo: encodeURIComponent($("#url_main_photo").val())
                }, function(res) {
                    if (res.type == 0) {
                        $("#folder_temp").val(res.folder_temp);
                        $("#file_main_photo_temp").val(res.file_name_temp);
                        $("#show_main_photo").html('<img src="' + res.thumb_url + '" width=200px />').show();

                    } else {
                        alert(res.msg);

                    }
                    upload_photo = false;
                }, "json");
            } else {
                alert("Vui lòng điền link photo");

                upload_photo = false;
            }
        }
    });
    //delete extra photo
    $(".btn_delete_extra_photo").click(function() {
        var photo_name = $(this).attr("rel");
        index = array_extra_photo_name.indexOf(photo_name);
        array_extra_photo_name.splice(index, 1);
        $(this).parent().parent().remove();
    });
    //save product
    var act_submit = false;
    $("#save_product").click(function() {
        if (act_submit === false) {
            act_submit = true;
            if (checkValidate("#alert-error-product")) {
                $.post("ajax/updateProduct.php", {
                    product_id: $("#product_id").val(),
                    product_name: $("#product_name").val(),
                    product_code: $("#product_code").val(),
                    link_web: $("#link_web").val(),
                    product_price: $("#product_price").val(),
                    store_price: $("#store_price").val(),
                    stamp_price: $("#stamp_price").val(),
                    product_size: $("#product_size").val(),
                    product_color: $("#product_color").val(),
                    manufacture: $("#manufacture").val(),
                    product_category: $("#product_category").val(),
                    folder_temp: $("#folder_temp").val(),
                    file_main_photo_temp: $("#file_main_photo_temp").val(),
                    array_extra_photo_name: array_extra_photo_name,
                    description: $("#description").val()
                }, function(res) {
                    act_submit = false;
                    if (res.type == 0) {
                        alert(res.msg);
                        if ($("#product_id").val() > 0) {
                            window.location = "productList.php";
                        } else {
                            refresh_form();
                        }
                    } else {
                        $("#alert-error-product").html(res.msg).show();
                        hideError("#alert-error-product");
                    }
                }, "json");

            } else {
                act_submit = false;
                return false;
            }
        } else {
            return false;
        }
    });
    //get product info by code
    var get_product_info = false;
    $("#get_product_info_by_code").click(function() {
        if (get_product_info === false) {
            get_product_info = true;
            if ($('#product_code').val() !== "") {
                getProductInfoByCode($('#product_code').val());
                get_product_info = false;
            } else {
                $("#alert-error-product_code").html("Vui lòng điền mã sản phẩm!").show();
                hideError("#alert-error-product");
                get_product_info = false;
            }
        } else {
            return false;
        }
    });
});
function getProductInfoByCode(code) {
    $.post("ajax/getProductInfoByCode.php", {
        product_code: code
    }, function(res) {
        if (res.type == 0) {
            $("#alert-error-product_code").html("Mã sản phẩm đã tồn tại, vui lòng nhập mã khác!").show();
            hideError("#alert-error-product");
        } else {
            $("#alert-error-product_code").html("Mã sản phẩm hợp lệ!").show();
            hideError("#alert-error-product");
        }

    }, "json");
}
function uploadFile() {
    $("#main_photo").change(function() {

        $("#form_upload_main_photo").submit();
    });
}

function checkValidate(id_append) {
    var emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
            numericRegex = /^[0-9-]+$/;
    var check_char = /^[a-zA-Z]+$/;
    var msgEr = '';
    var valueStr = '';
    if ($("#product_name").val() == '') {
        $(id_append).html("Vui lòng nhập tên sản phẩm!").show();
        actHideError(id_append, $("#product_name"));
        return false;
    }
    if ($("#product_code").val() == '') {
        $(id_append).html("Vui lòng nhập mã sản phẩm!").show();
        actHideError(id_append, $("#product_code"));
        return false;
    }
    if ($("#manufacture").val() == '') {
        $(id_append).html("Vui lòng nhập thương hiệu!").show();
        actHideError(id_append, $("#manufacture"));
        return false;
    }
    if ($("#product_category").val() == '') {
        $(id_append).html("Vui lòng nhập loại sản phẩm!").show();
        hideError(id_append);
        return false;
    }
    if ($("#file_main_photo_temp").val() == '') {
        $(id_append).html("Vui lòng chọn ảnh đại diện!").show();
        hideError(id_append);
        return false;
    }
    if ($("#product_color").val() == '') {
        $(id_append).html("Vui lòng nhập màu sản phẩm!").show();
        actHideError(id_append, $("#product_color"));
        return false;
    }

    return true;
}
function refresh_form() {
    $("#product_id").val(0);
    $("#product_name").val('');
    $("#product_code").val('');
    $("#link_web").val('');
    $("#product_price").val('');
    $("#store_price").val('');
    $("#stamp_price").val('');
    $("#product_size").val('');
    $("#product_color").val('');
    // $("#manufacture").val('');
    $("#product_category").val(0);
    $("#folder_temp").val('');
    $("#file_main_photo_temp").val('');
    $("#url_main_photo").val('');
    $(".filename", "#form_upload_main_photo").html("No file selected");
    $("#show_main_photo").hide();
    $(".filename", "#fileupload").html("No file selected");
    $("table", "#fileupload").remove();
    $("#description").val('');
    $("iframe", ".cleditorMain").contents().find("body").empty();
}

